<?php
!function_exists('adminmsg') && die('Forbidden');

require_once(D_P.'data/cache/cate.php');
require_once(R_P.'require/class_action.php');

/**
 * 处理栏目的内容在后台的展示
 *
 */
class ShowContent{

	var $catedb;
	var $moduledb;
	
	function __construct()
	{
		global $catedb,$moduledb;
		$this->catedb = $catedb;
		$this->moduledb = $moduledb;
	}	

	function doIt($action)
	{
		!$action && $action='show';
		switch ($action) {
			case 'show':
				$this->show();
				break;
			case 'view':
				$this->viewContent();
				break;
			case 'digest':
				$this->digest();
				break;
			default:
				$this->action();
				break;
		}
	}
	
	function show()
	{
		global $action,$db,$basename;
		$sqladd = '';
		$up = intval($_GET['up']);
		if (!$up)
		{
			$cname = 'ROOT';
			$sqladd = "WHERE up='0'";
		}
		else
		{
			$up = (int)$up;
			$sqladd = "WHERE up='$up'";
			$cname = $this->catedb[$up]['cname'];
		}
		$rs = $db->query("SELECT * FROM cms_category $sqladd ORDER BY taxis DESC");
		$category = array();
		while ($ct = $db->fetch_array($rs)) 
		{
			$ct['module']=$this->moduledb[$ct['mid']]['mname'];
			$category[]=$ct;
		}
		require PrintEot('header');
		require PrintEot('content');
		adminbottom();
	}
	
	function viewContent()
	{
		global $very,$db,$action,$basename,$displaynum,$cid,$keyword,$displaytype;
		$cid = intval($cid);
		if($this->catedb[$cid]['mid']=='-2' && (!$very['aggrebbs'] || !$very['bbs_dbname'])) showmsg('mod_needaggrebbs');
		if($this->catedb[$cid]['mid']=='-1' && (!$very['aggreblog'] || !$very['blog_dbname'])) showmsg('mod_needaggreblog');
		require_once(R_P.'require/class_cate.php');
		$cate = new Cate();
		$cate_select = $cate->tree();
		extract($db->get_one("SELECT * FROM cms_category WHERE cid='$cid'"));
		if(!$cname) Showmsg('pub_cateerror');
		$mid == 0 && Showmsg('pub_nocontent');

		if(!$displaynum)
		{
			$displaynum=30;
		}else {
			$numadd="displaynum=$displaynum&";
		}
		if($displaytype==1){
			$where="where:ifpub=1";
			$pubadd="displaytype=1&";
		}elseif ($displaytype==2){
			$where="where:ifpub=0";
			$pubadd="displaytype=2&";		
		}
		$keyword = Char_cv($keyword);
		if($keyword) {
			$keywordadd="keyword=$keyword&";
			$where = $where ? $where." AND title LIKE('%$keyword%')" : "WHERE:title LIKE('%$keyword%')";
		}
		if(!is_numeric($page) || $page<=0) $page=1;
		$start = ($page-1)*$displaynum;
		
		//$selectString = getResType();
		require_once(R_P.'require/class_cms.php');
		$cms = new Cms();
		$cms->pageurl="$basename&action=view&cid=$cid&$numadd$pubadd$keywordadd";
		//exit("cid:$cid;num:page-$displaynum;mid:$mid;order:ifpub;$where");
		$content = $cms->thread("cid:$cid;num:page-$displaynum;mid:$mid;order:ifpub,postdate DESC;$where");
		if($mid>0) $this->showDigest($content);
		require PrintEot('header');
		require PrintEot('content');
		adminbottom();		
	}
	
	function digest()
	{ //设定精华
		global $tid,$cid,$mid,$db,$digestnum;
		$digestnum = intval($digestnum);
		$tid = intval($tid);
		if($db->update("UPDATE cms_contentindex SET digest='$digestnum' WHERE tid='$tid'"))
		{
			exit("100");
		}
		else
		{
			exit("200");
		}
	}
	
	function showDigest(&$array)
	{ //展示精华推荐效果
		foreach ($array as $key=>$val){
			$img = "<div id='d_{$key}' oncontextmenu=\"digest('".$val['tid']."','".$key."','0');return false;\"><div onmouseout=\"reSet('$key','".$array[$key]['digest']."');\">";
			for($i=1;$i<=3;$i++){
				$imgname = $i<=$array[$key]['digest'] ? 'ok' : 'no';
				$img.="<img id='img{$key}_{$i}' class='st' src='images/admin/star_$imgname.gif' onmouseover=\"showStar('$key','$i');\" onclick=\"digest('".$val['tid']."','".$key."','".$i."');\" />";
			}
			/*
			if($array[$key]['digest']){
				$array[$key]['digest_c']="<img class='st' src=images/admin/star_c.gif onclick=\"digest('".$val['tid']."','".$key."','0');\" title='取消精华' />";
			}
			*/
			$img.="</div></div>";
			$array[$key]['digest'] = $img;
		}
	}
	
	function action()
	{
		global $cid,$tocid,$tid,$tids,$job,$basename;
		if(!$tids && $tid) $tids = $tid;
		$action = new Action($job);
		$action->cate($cid);
		$action->target($tocid);
		$action->doIt($tids);
		adminmsg('operate_success',"$basename&action=view&cid=$cid");		
	}
}

$showcontent = new ShowContent();
$showcontent->doIt($action);
?>